package com.zxy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxy.common.BindingResultUtil;
import com.zxy.common.ResultData;
import com.zxy.entity.College;
import com.zxy.service.ICollegeService;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Zxy
 * @since 2021-05-11
 */
@RestController
@RequestMapping("/college")
@AllArgsConstructor
public class CollegeController {

    private final ICollegeService collegeService;

    // 查询全部学院信息
    @GetMapping("/findall")
    public ResultData allCollege(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<College> page = new Page<>(current, size);
        Page<College> collegePage = collegeService.page(page);
        if (collegePage != null) {
            return ResultData.success(collegePage);
        } else {
            return ResultData.fail("数据获取失败，请联系网站管理员");
        }
    }

    // 新增学院信息
    @PostMapping("/addcollege")
    public ResultData addCollege(@Validated @RequestBody College college, BindingResult result) {
        BindingResultUtil.validate(result);
        boolean save = collegeService.save(college);
        if (save) {
            return ResultData.success(save);
        } else {
            return ResultData.fail("保存失败，请联系网站管理员");
        }
    }

    // 根据id删除学院信息
    @DeleteMapping("/deletebyid")
    public ResultData deleteById(@RequestParam("id") Integer id) {

        boolean remove = collegeService.removeById(id);
        if (remove) {
            return ResultData.success(remove);
        } else {
            return ResultData.fail("保存失败，请联系网站管理员");
        }
    }

    // 根据id修改学院信息
    @PutMapping("/updatebyid")
    public ResultData updateById(@Validated @RequestBody College college, BindingResult result) {
        BindingResultUtil.validate(result);
        boolean update = collegeService.updateById(college);
        if (update) {
            return ResultData.success(update);
        } else {
            return ResultData.fail("保存失败，请联系网站管理员");
        }
    }


}
